package rocketmq.demo.async.trans;

import java.util.Date;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendCallback;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.common.RemotingHelper;

public class AsyncProducer2 {
	public static void main(String[] args) throws Exception {
		DefaultMQProducer producer = new DefaultMQProducer("testGroup3");
		// 指定nameServer的地址 （测试时在代码中指定）
		producer.setNamesrvAddr("127.0.0.1:9876");
		producer.start();
		producer.setRetryTimesWhenSendAsyncFailed(0);
		for (int i = 0; i < 5; i++) {
			final int index = i;
			String content = "你好  RocketMQ, " + new Date().getTime() + "-->" + i;
			Message msg = new Message("TopicAsync", "testTag", "keys",
					content.getBytes(RemotingHelper.DEFAULT_CHARSET));
			producer.send(msg, new SendCallback() {
				@Override
				public void onSuccess(SendResult sendResult) {
					System.out.printf("发送成功：%d %s %n", index, sendResult);
				}

				@Override
				public void onException(Throwable e) {
					System.out.printf("发送失败：%d %s %n", index, e);
					e.printStackTrace();
				}
			});
		}
		producer.shutdown();
	}
}